import {ScoreType} from "@/components/ScorePanel/type";
import Players from "@/modal/Players";
import {type Reactive, reactive} from "vue";
import Buff from "@/modal/Buff";
import type Fruit from "@/modal/Fruit";
import type {Pumpkin} from "@/modal/GardenCrop";

const buff = reactive(Buff.getInstance());
export default class Score extends Players {
  private cornScore_: number = 0;
  private peaScore_: number = 0;
  private pumpkinScore_: number = 0;
  private flowerScore_: number = 0;
  private tulipScore_: number = 0;
  private beehiveScore_: number = 0;
  private fruitScore_: number = 0;
  private saladScore_: number = 0;
  private toolScore_: number = 0;


  public addScore = (score: number, type: ScoreType) => {
    switch (type) {
      case ScoreType.CORN:
        this.cornScore_ += score;
        console.log(`菜园分数:${this.gardenScore},全局总分:${this.totalScore}`);
        break;
      case ScoreType.PEA:
        this.peaScore_ += score;
        console.log(`菜园分数:${this.gardenScore},全局总分:${this.totalScore}`);
        break;
      case ScoreType.FLOWER:
        this.flowerScore_ += score;
        console.log(`花卉分数:${this.flowerScore},全局总分:${this.totalScore}`);
        break;
      case ScoreType.BEEHIVE:
        this.beehiveScore_ += score;
        console.log(`蜂巢分数:${this.beehiveScore},全局总分:${this.totalScore}`);
        break;
      case ScoreType.FRUIT:
        this.fruitScore_ += score;
        console.log(`果园分数:${this.fruitScore},全局总分:${this.totalScore}`);
        break;
      case ScoreType.TOOL:
        this.toolScore_ += score;
        console.log(`工具分数:${this.toolScore},全局总分:${this.totalScore}`);
        break;
    }
  }

  // 果盘分数
  public getSaladScore(arr: Reactive<Fruit[]>) {
    let kindsNum: number = 0;
    arr.forEach(items => {
      items.ripenNumber > 0 ? kindsNum++ : kindsNum;
    })
    this.saladScore_ = kindsNum > 0 ? kindsNum * 3 - 2 : 0;
    console.log(`果盘得分:${this.saladScore_},果园总分:${this.fruitScore},全局总分:${this.totalScore}`);
  }

  // 南瓜刻刀
  public getPumpkinKnifeScore(arr: Reactive<Pumpkin[]>) {
    const length = arr.length;
    const isCircle = arr[0].isRipen && arr[length - 1].isRipen;  // 头尾是否相接
    let maxLength = 0,
      eachLength = 0,       // 循环时每段长度
      interruptTimes = 0,   // 中断次数
      firstLength = 0,      // 以0为开始后，首段长度
      lastLength = 0;       // 末段长度
    arr.forEach((items, index) => {
      if (items.isRipen) {
        eachLength++;
        maxLength = Math.max(maxLength, eachLength);
        if (interruptTimes === 0) { // 判断是否还未被打断
          firstLength = eachLength;
        }
        if (interruptTimes > 0 && index > 0 && eachLength > 0) { // 记录最后一次被打断前不为零的长度
          lastLength = eachLength;
        }
      } else {
        interruptTimes++;
        eachLength = 0;
      }
    })
    // 头尾相连则再次计算
    if (isCircle) {
      let circleLength = firstLength + lastLength;
      maxLength = Math.max(maxLength, circleLength);
    }
    this.pumpkinScore_ = maxLength;
    console.log(`南瓜藤得分:${this.pumpkinScore_},菜园总分:${this.gardenScore},全局总分:${this.totalScore}`)
  }

  // 郁金香分数
  public getTupleScore(fruits: number) {
    this.tulipScore_ = buff.tulipScore * fruits > 15 ? 15 : fruits;
    console.log(`郁金香得分:${this.tulipScore_},果园总分:${this.fruitScore},全局总分:${this.totalScore}`);
  }

  get gardenScore(): number {
    return this.cornScore_ +
      this.peaScore_ +
      this.pumpkinScore_;
  }

  get flowerScore(): number {
    return this.flowerScore_ +
      this.tulipScore_;
  }

  get beehiveScore(): number {return this.beehiveScore_;}

  get fruitScore(): number {
    return this.fruitScore_ +
      this.saladScore_;
  }

  get toolScore(): number {return this.toolScore_;}

  get totalScore(): number {
    return this.gardenScore +
      this.flowerScore +
      this.beehiveScore +
      this.fruitScore +
      this.toolScore;
  }

  // 单例模式
  private constructor() {super();}

  private static instance: Score | null = null;

  static getInstance(): Score {
    if (this.instance === null) {
      this.instance = new Score();
    }
    return this.instance;
  }
}
